package cn.stylefeng.guns.modular.scheduleInfo.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.scheduleInfo.entity.ScheduleInfo;
import cn.stylefeng.guns.modular.scheduleInfo.model.params.ScheduleAttendanceParam;
import cn.stylefeng.guns.modular.scheduleInfo.model.params.ScheduleDetailInfoParam;
import cn.stylefeng.guns.modular.scheduleInfo.model.params.ScheduleInfoParam;
import cn.stylefeng.guns.modular.scheduleInfo.model.params.ScheduleInfoPlantParam;
import cn.stylefeng.guns.modular.scheduleInfo.model.result.ScheduleAttendanceResult;
import cn.stylefeng.guns.modular.scheduleInfo.model.result.ScheduleDetailInfoResult;
import cn.stylefeng.guns.modular.scheduleInfo.model.result.ScheduleInfoResult;
import cn.stylefeng.guns.modular.scheduleInfo.service.ScheduleAttendanceService;
import cn.stylefeng.guns.modular.scheduleInfo.service.ScheduleDetailInfoService;
import cn.stylefeng.guns.modular.scheduleInfo.service.ScheduleInfoService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.Duration;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 排班主表控制器
 *
 * @author wp
 * @Date 2025-03-04 10:30:34
 */
@Controller
@RequestMapping("/scheduleInfo")
public class ScheduleInfoController extends BaseController {

    private String PREFIX = "/scheduleInfo";

    @Autowired
    private ScheduleInfoService scheduleInfoService;
    @Autowired
    private ScheduleDetailInfoService scheduleDetailInfoService;

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;

    @Autowired
    private ScheduleAttendanceService scheduleAttendanceService;

    private int anInt;

    /**
     * 跳转到主页面
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/scheduleInfo.html";
    }

    /**
     * 新增页面
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/scheduleInfo_add.html";
    }
    /**
     * 排班页面
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/plant")
    public String plant() {
        return PREFIX + "/scheduleInfo_plant.html";
    }

    /**
     * 排班页面
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/updatePlant")
    public String updatePlant() {
        return PREFIX + "/scheduleInfo_update_plant.html";
    }

    /**
     * 编辑页面
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/scheduleInfo_edit.html";
    }

    /**
     * 编辑页面
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/scheduleInfo_detail")
    public String plantDetail() {
        return PREFIX + "/scheduleInfo_detail.html";
    }

    /**
     * 新增接口
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ScheduleInfoParam scheduleInfoParam) {

        List<ScheduleInfoResult> listBySpec = this.scheduleInfoService.findListBySpec(scheduleInfoParam);
        if (listBySpec.size() > 0) {
            return ResponseData.error("该日期已存在排班信息，请勿重复添加！");
        }
        //预估时效
        String estimatedTimeFrame = scheduleInfoParam.getEstimatedTimeFrame();
        //预估营业额
         String estimatedTurnover = scheduleInfoParam.getEstimatedTurnover();

        int i = Integer.valueOf(estimatedTurnover) / Integer.valueOf(estimatedTimeFrame);

        scheduleInfoParam.setEstimatedWorkingHours(i+"");
        this.scheduleInfoService.add(scheduleInfoParam);
        return ResponseData.success();
    }




    /**
     * 编辑接口
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ScheduleInfoParam scheduleInfoParam) {
        this.scheduleInfoService.update(scheduleInfoParam);
        return ResponseData.success();
    }

    /**
     * 新增排班接口
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/addPlant")
    @ResponseBody
    public ResponseData addPlant(ScheduleInfoPlantParam scheduleInfoParam) {



        //唯一性校验
        ScheduleDetailInfoParam scheduleDetailInfoParam = new ScheduleDetailInfoParam();
        scheduleDetailInfoParam.setPlantDate(scheduleInfoParam.getPlantDate());
        scheduleDetailInfoParam.setEmpName(scheduleInfoParam.getEmpName());
        scheduleDetailInfoParam.setStoreName(scheduleInfoParam.getStoreName());


        List<ScheduleDetailInfoResult> scheduleDetailInfoResults = this.scheduleDetailInfoService.checkUnique(scheduleDetailInfoParam);
        if(scheduleDetailInfoResults.size() > 0){
            return ResponseData.error("该日期已排班，请勿重复排班！");
        }

        ScheduleDetailInfoParam param = new ScheduleDetailInfoParam();
        param.setStoreName(scheduleInfoParam.getStoreName());
        param.setPlantDate(scheduleInfoParam.getPlantDate());
        param.setEmpName(scheduleInfoParam.getEmpName());
        param.setPosition(scheduleInfoParam.getPosition());
        String planTime = scheduleInfoParam.getPlanTime();
        String[] split = planTime.split("-");
        String startTime = split[0];
        String endTime = split[1];
        List<String> strings = tranceDate(startTime, endTime);
        AtomicInteger actualHour = new AtomicInteger();
        strings.forEach(s -> {
            switch (s){
                case "08:00 - 08:30" :
                    param.setClock0830("1");
                    actualHour.getAndIncrement();
                    break;
                case "08:30 - 09:00" :
                    param.setClock0900("1");
                    actualHour.getAndIncrement();
                    break;
                case "09:00 - 09:30" :
                    param.setClock0930("1");
                    actualHour.getAndIncrement();
                    break;
                case "09:30 - 10:00" :
                    param.setClock1000("1");
                    actualHour.getAndIncrement();
                    break;
                case "10:00 - 10:30" :
                    param.setClock1030("1");
                    actualHour.getAndIncrement();
                    break;
                case "10:30 - 11:00" :
                    param.setClock1100("1");
                    actualHour.getAndIncrement();
                    break;
                case "11:00 - 11:30" :
                    param.setClock1130("1");
                    actualHour.getAndIncrement();
                    break;
                case "11:30 - 12:00" :
                    param.setClock1200("1");
                    actualHour.getAndIncrement();
                    break;
                case "12:00 - 12:30" :
                    param.setClock1230("1");
                    actualHour.getAndIncrement();
                    break;
                case "12:30 - 13:00" :
                    param.setClock1300("1");
                    actualHour.getAndIncrement();
                    break;
                case "13:00 - 13:30" :
                    param.setClock1330("1");
                    actualHour.getAndIncrement();
                    break;
                case "13:30 - 14:00" :
                    param.setClock1400("1");
                    actualHour.getAndIncrement();
                    break;
                case "14:00 - 14:30" :
                    param.setClock1430("1");
                    actualHour.getAndIncrement();
                    break;
                case "14:30 - 15:00" :
                    param.setClock1500("1");
                    actualHour.getAndIncrement();
                    break;
                case "15:00 - 15:30" :
                    param.setClock1530("1");
                    actualHour.getAndIncrement();
                    break;
                case "15:30 - 16:00" :
                    param.setClock1600("1");
                    actualHour.getAndIncrement();
                    break;
                case "16:00 - 16:30" :
                    param.setClock1630("1");
                    actualHour.getAndIncrement();
                    break;
                case "16:30 - 17:00" :
                    param.setClock1700("1");
                    actualHour.getAndIncrement();
                    break;
                case "17:00 - 17:30" :
                    param.setClock1730("1");
                    actualHour.getAndIncrement();
                    break;
                case "17:30 - 18:00" :
                    param.setClock1800("1");
                    actualHour.getAndIncrement();
                    break;
                case "18:00 - 18:30" :
                    param.setClock1830("1");
                    actualHour.getAndIncrement();
                    break;
                case "18:30 - 19:00" :
                    param.setClock1900("1");
                    actualHour.getAndIncrement();
                    break;
                case "19:00 - 19:30" :
                    param.setClock1930("1");
                    actualHour.getAndIncrement();
                    break;
                case "19:30 - 20:00" :
                    param.setClock2000("1");
                    actualHour.getAndIncrement();
                    break;
                case "20:00 - 20:30" :
                    param.setClock2030("1");
                    actualHour.getAndIncrement();
                    break;
                case "20:30 - 21:00" :
                    param.setClock2100("1");
                    actualHour.getAndIncrement();
                    break;
                case "21:00 - 21:30" :
                    param.setClock2130("1");
                    actualHour.getAndIncrement();
                    break;
                case "21:30 - 22:00" :
                    param.setClock2200("1");
                    actualHour.getAndIncrement();
                    break;
                case "22:00 - 22:30" :
                    param.setClock2230("1");
                    actualHour.getAndIncrement();
                    break;

                case "22:30 - 23:59" :
                    param.setClock2400("1");
                    actualHour.getAndIncrement();
                    break;
            }
        });
        param.setActualHour(actualHour.get()/2+"");
//        ScheduleDetailInfoResult detail = this.scheduleDetailInfoService.findListBySpec(param);
//        List<ScheduleDetailInfoResult> detail = this.scheduleDetailInfoService.findListBySpec(param);
//        if(detail.size() > 0){
//            return ResponseData.error("该日期已排班，请勿重复排班！");
//        }


        this.scheduleDetailInfoService.add(param);

        //更新主表
//        根据门店、排班日期获取总表数据
        ScheduleInfoParam scheduleInfoParams = new ScheduleInfoParam();
        scheduleInfoParams.setStoreName(scheduleInfoParam.getStoreName());
        scheduleInfoParams.setPlantDate(scheduleInfoParam.getPlantDate());

        List<ScheduleInfoResult> listBySpec = this.scheduleInfoService.findListBySpec(scheduleInfoParams);
        ScheduleInfoResult scheduleInfoResult = listBySpec.get(0);
        Integer id = scheduleInfoResult.getId();
        String actualHour1 = scheduleInfoResult.getActualHour();
        ScheduleInfo scheduleInfo = new ScheduleInfo();
        scheduleInfo.setId(id);
        scheduleInfo.setActualHour(Integer.valueOf(param.getActualHour())+Integer.valueOf(actualHour1) +"");
        this.scheduleInfoService.updateById(scheduleInfo);
        //        this.scheduleInfoService.add(scheduleInfoParam);
        return ResponseData.success();
    }


    /**
     * 修改排班接口
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/updatePlantInfo")
    @ResponseBody
    public ResponseData updatePlantInfo(ScheduleInfoPlantParam scheduleInfoParam) {

        //唯一性校验
        ScheduleDetailInfoParam scheduleDetailInfoParam = new ScheduleDetailInfoParam();
        scheduleDetailInfoParam.setPlantDate(scheduleInfoParam.getPlantDate());
        scheduleDetailInfoParam.setEmpName(scheduleInfoParam.getEmpName());
        scheduleDetailInfoParam.setStoreName(scheduleInfoParam.getStoreName());

        List<ScheduleDetailInfoResult> scheduleDetailInfoResults = this.scheduleDetailInfoService.checkUnique(scheduleDetailInfoParam);
        if(scheduleDetailInfoResults.size() == 0){
            return ResponseData.error("该日期当前人员未排班，不需要修改排班！");
        }
        scheduleDetailInfoResults.forEach(scheduleDetailInfoResult -> {
            ScheduleDetailInfoParam scheduleDetailInfoParam1 = new ScheduleDetailInfoParam();
            scheduleDetailInfoParam1.setId(scheduleDetailInfoResult.getId());
            this.scheduleDetailInfoService.delete(scheduleDetailInfoParam1);
        });
        //        if(scheduleDetailInfoResults.size() > 0){
//            return ResponseData.error("该日期已排班，请勿重复排班！");
//        }

        ScheduleDetailInfoParam param = new ScheduleDetailInfoParam();
        param.setStoreName(scheduleInfoParam.getStoreName());
        param.setPlantDate(scheduleInfoParam.getPlantDate());
        param.setEmpName(scheduleInfoParam.getEmpName());
        param.setPosition(scheduleInfoParam.getPosition());
        String planTime = scheduleInfoParam.getPlanTime();
        String[] split = planTime.split("-");
        String startTime = split[0];
        String endTime = split[1];
        List<String> strings = tranceDate(startTime, endTime);
        AtomicInteger actualHour = new AtomicInteger();
        strings.forEach(s -> {
            switch (s){
                case "08:00 - 08:30" :
                    param.setClock0830("1");
                    actualHour.getAndIncrement();
                    break;
                case "08:30 - 09:00" :
                    param.setClock0900("1");
                    actualHour.getAndIncrement();
                    break;
                case "09:00 - 09:30" :
                    param.setClock0930("1");
                    actualHour.getAndIncrement();
                    break;
                case "09:30 - 10:00" :
                    param.setClock1000("1");
                    actualHour.getAndIncrement();
                    break;
                case "10:00 - 10:30" :
                    param.setClock1030("1");
                    actualHour.getAndIncrement();
                    break;
                case "10:30 - 11:00" :
                    param.setClock1100("1");
                    actualHour.getAndIncrement();
                    break;
                case "11:00 - 11:30" :
                    param.setClock1130("1");
                    actualHour.getAndIncrement();
                    break;
                case "11:30 - 12:00" :
                    param.setClock1200("1");
                    actualHour.getAndIncrement();
                    break;
                case "12:00 - 12:30" :
                    param.setClock1230("1");
                    actualHour.getAndIncrement();
                    break;
                case "12:30 - 13:00" :
                    param.setClock1300("1");
                    actualHour.getAndIncrement();
                    break;
                case "13:00 - 13:30" :
                    param.setClock1330("1");
                    actualHour.getAndIncrement();
                    break;
                case "13:30 - 14:00" :
                    param.setClock1400("1");
                    actualHour.getAndIncrement();
                    break;
                case "14:00 - 14:30" :
                    param.setClock1430("1");
                    actualHour.getAndIncrement();
                    break;
                case "14:30 - 15:00" :
                    param.setClock1500("1");
                    actualHour.getAndIncrement();
                    break;
                case "15:00 - 15:30" :
                    param.setClock1530("1");
                    actualHour.getAndIncrement();
                    break;
                case "15:30 - 16:00" :
                    param.setClock1600("1");
                    actualHour.getAndIncrement();
                    break;
                case "16:00 - 16:30" :
                    param.setClock1630("1");
                    actualHour.getAndIncrement();
                    break;
                case "16:30 - 17:00" :
                    param.setClock1700("1");
                    actualHour.getAndIncrement();
                    break;
                case "17:00 - 17:30" :
                    param.setClock1730("1");
                    actualHour.getAndIncrement();
                    break;
                case "17:30 - 18:00" :
                    param.setClock1800("1");
                    actualHour.getAndIncrement();
                    break;
                case "18:00 - 18:30" :
                    param.setClock1830("1");
                    actualHour.getAndIncrement();
                    break;
                case "18:30 - 19:00" :
                    param.setClock1900("1");
                    actualHour.getAndIncrement();
                    break;
                case "19:00 - 19:30" :
                    param.setClock1930("1");
                    actualHour.getAndIncrement();
                    break;
                case "19:30 - 20:00" :
                    param.setClock2000("1");
                    actualHour.getAndIncrement();
                    break;
                case "20:00 - 20:30" :
                    param.setClock2030("1");
                    actualHour.getAndIncrement();
                    break;
                case "20:30 - 21:00" :
                    param.setClock2100("1");
                    actualHour.getAndIncrement();
                    break;
                case "21:00 - 21:30" :
                    param.setClock2130("1");
                    actualHour.getAndIncrement();
                    break;
                case "21:30 - 22:00" :
                    param.setClock2200("1");
                    actualHour.getAndIncrement();
                    break;
                case "22:00 - 22:30" :
                    param.setClock2230("1");
                    actualHour.getAndIncrement();
                    break;

                case "22:30 - 23:59" :
                    param.setClock2400("1");
                    actualHour.getAndIncrement();
                    break;
            }
        });
        param.setActualHour(actualHour.get()/2+"");

        this.scheduleDetailInfoService.add(param);

        //更新主表
//        根据门店、排班日期获取总表数据
        ScheduleInfoParam scheduleInfoParams = new ScheduleInfoParam();
        scheduleInfoParams.setStoreName(scheduleInfoParam.getStoreName());
        scheduleInfoParams.setPlantDate(scheduleInfoParam.getPlantDate());

        List<ScheduleInfoResult> listBySpec = this.scheduleInfoService.findListBySpec(scheduleInfoParams);
        ScheduleInfoResult scheduleInfoResult = listBySpec.get(0);
        Integer id = scheduleInfoResult.getId();
        String actualHour1 = scheduleInfoResult.getActualHour();
        ScheduleInfo scheduleInfo = new ScheduleInfo();
        scheduleInfo.setId(id);
        String oldActualHour = scheduleDetailInfoResults.get(0).getActualHour();

        scheduleInfo.setActualHour(Integer.valueOf(param.getActualHour())+Integer.valueOf(actualHour1)-Integer.valueOf(oldActualHour) +"");
        this.scheduleInfoService.updateById(scheduleInfo);
        return ResponseData.success();
    }
    /**
     * 删除接口
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ScheduleInfoParam scheduleInfoParam) {
        this.scheduleInfoService.delete(scheduleInfoParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ScheduleInfoParam scheduleInfoParam) {
        ScheduleInfo detail = this.scheduleInfoService.getById(scheduleInfoParam.getId());
        ScheduleAttendanceParam param = new ScheduleAttendanceParam();
        param.setStoreName(detail.getStoreName());
        param.setPlantDate(detail.getPlantDate());
        List<ScheduleAttendanceResult> listBySpec = scheduleAttendanceService.findListBySpec(param);
        Long attendanceActualHour = 0L;
        for (ScheduleAttendanceResult scheduleAttendanceResult : listBySpec) {
            String actualHour = scheduleAttendanceResult.getActualHour();
            Long intActualHour = Long.valueOf(actualHour);
            attendanceActualHour += intActualHour;
        }

        detail.setAttendanceActualHour(attendanceActualHour);
        return ResponseData.success(detail);
    }

    /**
     * 排班明细页面
     *
     * @author wp
     * @Date 2025-03-04
     */
    @RequestMapping("/plantDetail")
    @ResponseBody
    public ResponseData plantDetail(ScheduleDetailInfoParam param) {

        ScheduleInfo scheduleInfo = this.scheduleInfoService.getById(param.getId());
        ScheduleDetailInfoParam detailParam = new ScheduleDetailInfoParam();
        detailParam.setStoreName(scheduleInfo.getStoreName());
        detailParam.setPlantDate(scheduleInfo.getPlantDate());

        List<ScheduleDetailInfoResult> detail = this.scheduleDetailInfoService.findListBySpec(detailParam);
        return ResponseData.success(0,"成功",detail);
    }
    /**
     * 查询列表
     *
     * @author wp
     * @Date 2025-03-04
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ScheduleInfoParam scheduleInfoParam) {
        Long deptId = userService.getDeptId();
        Dept bydeptId = deptService.getBydeptId(deptId);
        if(bydeptId != null && !bydeptId.getSimpleName().equals( "门店排班项目")){
            scheduleInfoParam.setStoreName(bydeptId.getSimpleName());
        }

        return this.scheduleInfoService.findPageBySpec(scheduleInfoParam);
    }



    public static void main(String[] args) {
        // 定义时间范围
        LocalTime startTime = LocalTime.parse("23:00:00");
        LocalTime endTime = LocalTime.parse("23:59:59");
        LocalTime endTime1 = LocalTime.parse("00:30:00");

        // 每半小时分割
        Duration interval = Duration.ofMinutes(30);
        List<String> timeRanges = new ArrayList<>();
        List<String> timeRanges1 = new ArrayList<>();

        // 循环生成时间范围
        LocalTime currentTime = startTime;
        while (currentTime.isBefore(endTime)) {
            LocalTime nextTime = currentTime.plus(interval);
            if (nextTime.isAfter(endTime)) {
                nextTime = endTime; // 确保不超过结束时间
            }
            timeRanges.add(currentTime.toString() + " - " + nextTime.toString());
            timeRanges1.add(nextTime.toString().replace(":",""));
            currentTime = nextTime;
            if(currentTime.isBefore(endTime1)){
                break;
            }
        }
        System.out.println("timeRanges = " + timeRanges);
        System.out.println("timeRanges = " + timeRanges1);
        // 输出结果
        timeRanges.forEach(System.out::println);
    }

    List<String> tranceDate(String startDate,String endDate){
        // 定义时间范围
        LocalTime startTime = LocalTime.parse(startDate.trim());
        LocalTime endTime = LocalTime.parse(endDate.trim());
        LocalTime endTime1 = LocalTime.parse("00:30:00");
        // 每半小时分割
        Duration interval = Duration.ofMinutes(30);
        List<String> timeRanges = new ArrayList<>();
        List<String> timeRanges1 = new ArrayList<>();

        // 循环生成时间范围
        LocalTime currentTime = startTime;
        while (currentTime.isBefore(endTime)) {
            LocalTime nextTime = currentTime.plus(interval);
            if (nextTime.isAfter(endTime)) {
                nextTime = endTime; // 确保不超过结束时间
            }
            timeRanges.add(currentTime.toString() + " - " + nextTime.toString());
            timeRanges1.add(nextTime.toString().replace(":",""));
            currentTime = nextTime;
            if(currentTime.isBefore(endTime1)){
                break;
            }
        }
        System.out.println("timeRanges = " + timeRanges);
        System.out.println("timeRanges = " + timeRanges1);
        // 输出结果
        timeRanges.forEach(System.out::println);
        return timeRanges;
    }
}


